#define _CRT_SECURE_NO_WARNINGS 1
#include "Sort.h"
#include "Stack.h"
#include "Queue.h"

//打印
void Print(int* a, int n)
{
	int i = 0;
	for (i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}

//交换
void Swap(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

//冒泡排序
void BubbleSort(int* a, int n)
{
	//打印未排序的数据
	Print(a, n);

	int j = 0;
	for (j = 0; j < n; j++)
	{
		//一趟交换
		int i = 0;
		int flag = 0;//标记，判断一趟中是否发生交换
		for (i = 1; i < n - j; i++)
		{
			if (a[i - 1] > a[i])
			{
				flag = 1;
				Swap(&a[i - 1], &a[i]);//交换
			}
		}
		//序列已经有序，不必在进行排序
		if (flag == 0)
		{
			break;
		}
	}

	//打印排序好的数据
	Print(a, n);
}

//插入排序
void InsertSort(int* a, int n)
{
	int i = 0;
	for (i = 0; i < n - 1; i++)
	{
		int end = i;
		int tmp = a[end + 1];//待插入的数
		while (end >= 0)
		{
			if (a[end] > tmp)
			{
				a[end + 1] = a[end];//将大数往后放
				end--;
			}
			else
			{
				break;
			}
		}
		a[end + 1] = tmp;
	}
}


// 希尔排序
void ShellSort(int* a, int n)
{
	int gap = n;
	while (gap > 1)
	{
		gap = gap / 3 + 1;

		int i = 0;
		for (i = 0; i < n - gap; i++)
		{
			int end = i;
			int tmp = a[end + gap];
			while (end >= 0)
			{
				if (tmp < a[end])
				{
					a[end+ gap] = a[end];
					end -= gap;
				}
				else
				{
					break;
				}
			}
			a[end + gap] = tmp;
		}
	}
}

//选择排序
void OldSelectSort(int* a, int n)
{
	int i = 0;
	for (i = 0; i < n; i++)
	{
		int mini = i;
		int j = 0;
		for (j = i; j < n; j++)
		{
			if (a[mini] > a[j])
			{
				mini = j;
			}
		}
		Swap(&a[i], &a[mini]);
	}
}

//优化选择排序
void OptimizeSelectSort(int* a, int n)
{
	int begin = 0;
	int end = n - 1;
	while (begin < end)
	{
		int mini = begin;//记录最小数据的下标
		int maxi = end;//记录最大数据的下标

		int j = 0;
		for (j = begin; j <= end; j++)
		{
			if (a[mini] > a[j])
			{
				mini = j;
			}
			if (a[maxi] < a[j])
			{
				maxi = j;
			}
		}
		if (mini + maxi == n - 1)
		{
			Swap(&a[mini], &a[maxi]);
		}
		else if (maxi == begin)
		{
			Swap(&a[end], &a[maxi]);
			Swap(&a[begin], &a[mini]);

		}
		else if (mini == end)
		{
			Swap(&a[begin], &a[mini]);
			Swap(&a[end], &a[maxi]);
		}


		begin++;
		end--;
	}
}


//向下调整算法
void AdjustDown(int* a, int parent, int size)
{
	assert(a);

	//小堆，与左右孩子中较小的交换
	//大堆，与左右孩子中较大的交换
	//假设法
	int child = parent * 2 + 1;
	while (child < size)
	{
		if (a[child] < a[child + 1] && child + 1 < size)//此时为大堆
		{
			child += 1;
		}
		if (a[parent] < a[child])//此时为大堆
		{
			//交换
			Swap(&(a[parent]), &(a[child]));
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}

//堆排序
void HeapSort(int* a, int n)
{
	assert(a);

	//向下调整建堆
	int i = 0;
	for (i = (n - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(a, i, n);
	}

	//交换数据，并向下调整
	int end = n - 1;
	while (end)
	{
		//交换数据
		Swap(&a[0], &a[end]);

		//向下调整
		AdjustDown(a, 0, end);

		end--;
	}
}

//三数取中法
int GetMidData(int* a,int left,int right)
{
	int begin = left;
	int end = right;
	int mid = (left + right) / 2;

	if (a[begin] > a[mid])
	{
		if (a[mid] > a[end])
		{
			//begin > mid > end
			return mid;
		}
		else
		{
			//mid是最小的，返回end和begin中较小的那个
			return a[begin] > a[end] ? end : begin;
		}
	}
	else//begin < mid
	{
		if (a[mid] < a[end])
		{
			//begin < mid < end
			return mid;
		}
		else
		{
			//mid是最大的，返回begin和end中较大的那个
			return a[begin] > a[end] ? begin : end;
		}
	}
}

//hoare快排
int hoareSort(int* a, int left, int right)
{
	//三数取中法
	int mid = GetMidData(a, left, right);
	Swap(&a[mid], &a[left]);//将中间数的值与left的值进行交换，left的值就一定不是最小的了。

	int begin = left;
	int end = right;
	int keyi = left;

	while (begin < end)
	{
		//end先往左走，找小
		while (a[end] > a[keyi] && begin < end)
		{
			end--;
		}

		//begin往右走，找大
		while (a[begin] < a[keyi] && begin < end)
		{
			begin++;
		}

		//begin和end在一个位置时交换没必要
		if (begin != end)
		{
			//交换end和begin处的数据
			Swap(&a[begin], &a[end]);
		}
	}

	//此时begin和end相遇，交换其和keyi的值
	Swap(&a[begin], &a[keyi]);
	keyi = begin;

	return keyi;
}

//挖坑法
int DigHoleSort(int* a, int left, int right)
{
	//三数取中法
	int mid = GetMidData(a, left, right);

	//将中间数的值与left的值进行交换，left的值就一定不是最小的了
	Swap(&a[mid], &a[left]);

	int begin = left;
	int end = right;
	int key = a[left];//关键字，R和L比较的标准
	int hole = left;//在挖坑法中hole就是坑位的下标

	while (begin < end)
	{
		while (a[end] > key && begin < end)
		{
			end--;
		}

		//找到小于key的数之后填入坑中
		a[hole] = a[end];

		//更新坑位
		hole = end;

		while (a[begin] < key && begin < end)
		{
			begin++;
		}

		//找到大于key的数之后填入坑中
		a[hole] = a[begin];

		//更新坑位
		hole = begin;
	}
	//相遇后，将key填入坑中
	a[hole] = key;

	//hole下标对应的数据就已经有序了
	return hole;
}

//前后指针法
int PrevCurSort(int* a, int left, int right)
{
	//三数取中法
	int mid = GetMidData(a, left, right);

	//将中间数的值与left的值进行交换，left的值就一定不是最小的了
	Swap(&a[mid], &a[left]);

	int prev = left;
	int cur = prev + 1;
	int keyi = left;

	while (cur <= right)
	{	
		//cur小于key时，prev向后走一步，然后交换
		if (a[cur] < a[keyi] && ++prev != cur)
		{
			Swap(&a[cur], &a[prev]);
		}
		cur++;
	}
	Swap(&a[prev], &a[keyi]);
	keyi = prev;

	return keyi;
}


//快排
void QuickSort(int* a, int left, int right)//left为起始下标，right为终止下标
{
	//区间只有一个数或者没有数时不需要排序
	if (left >= right)
	{
		return;
	}

	//小区间优化
	if ((right - left + 1) < 10)
	{
		InsertSort(a+left, right - left + 1);
	}
	else 
	{
		//int keyi = hoareSort(a, left, right);
		int keyi = DigHoleSort(a, left, right);

		//一个单趟走完，此时keyi处的数据已经有序，将还需排序的数据分成了两个区间，再对下面区间数据采用相同逻辑进行排序
		//[left,keyi-1] keyi [keyi+1,right]
		QuickSort(a, left, keyi - 1);
		QuickSort(a, keyi + 1, right);
	}
}

//非递归快排
void QuickSortNonRByStack(int* a, int left, int right)
{
	//创建栈
	Stack s = { 0 };

	StackInit(&s);

	//先将区间端点压入栈中
	//先压右端点后压左端点
	StackPush(&s, right);
	StackPush(&s, left);

	while (!StackEmpty(&s))
	{
		int begin = GetStackTop(&s);
		StackPop(&s);
		int end = GetStackTop(&s);
		StackPop(&s);

		int keyi = PrevCurSort(a, begin, end);

		//右区间
		if (end > keyi + 1)
		{
			StackPush(&s, end);
			StackPush(&s, keyi + 1);
		}
		//左区间
		if (begin < keyi - 1)
		{
			StackPush(&s, keyi - 1);
			StackPush(&s, begin);
		}
	}

	StackDestory(&s);
}

//借助队列实现非递归快排
void QuickSortNonRByQueue(int* a, int left, int right)
{
	//创建队列
	Queue q = { 0 };

	QueueInit(&q);

	QueuePush(&q, left);
	QueuePush(&q, right);

	while (!QueueEmpty(&q))
	{
		//获取区间左右端点
		int begin = GetQueueFront(&q);
		QueuePop(&q);
		int end = GetQueueFront(&q);
		QueuePop(&q);

		int keyi = PrevCurSort(a, begin, end);

		//先入左区间
		if (begin < keyi - 1)
		{
			//先入左端点
			QueuePush(&q, begin);
			QueuePush(&q, keyi - 1);
		}
		if (end > keyi + 1)
		{
			QueuePush(&q, keyi + 1);
			QueuePush(&q, end);
		}
	}


	QueueDestroy(&q);
}

void _MergeSort(int* a, int* tmp, int left, int right)
{
	if (left >= right)
	{
		return;
	}

	//分割区间
	//[left,mid][mid+1,right]
	int mid = (left + right) / 2;

	_MergeSort(a, tmp, left, mid);
	_MergeSort(a, tmp, mid + 1, right);

	//归并
	int i = left;

	//左区间
	int begin1 = left;
	int end1 = mid;

	//右区间
	int begin2 = mid + 1;
	int end2 = right;

	while (begin1 <= end1 && begin2 <= end2)
	{
		if (a[begin1] < a[begin2])
		{
			tmp[i++] = a[begin1++];
		}
		else
		{
			tmp[i++] = a[begin2++];
		}
	}
	//一个区间结束放完了，还有一个区间没完
	while (begin1 <= end1)
	{
		tmp[i++] = a[begin1++];
	}
	while (begin2 <= end2)
	{
		tmp[i++] = a[begin2++];
	}

	memcpy(a + left, tmp + left, sizeof(int) * (end2 - left + 1));
}

//归并排序——递归
void MergeSort(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	if (tmp == NULL)
	{
		perror("MergeSort()::malloc()");
		return;
	}
	_MergeSort(a,tmp,0,n-1);

	free(tmp);
	tmp = NULL;
}


//归并排序——非递归
void MergeSortNonR(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	if (tmp == NULL)
	{
		perror("MergeSort()::malloc()");
		return;
	}

	int gap = 1;
	while (gap < n)
	{
		int i = 0;
		for (i = 0; i < n; i += 2*gap)
		{
			int begin1 = i;
			int end1 = i + gap - 1;
			int begin2 = i + gap;
			int end2 = i + 2 * gap - 1;

			//printf("[%d,%d] [%d,%d]", begin1, end1, begin2, end2);
			//begin2或者end1越界，都不需要归并，直接跳出循环
			if (begin2 >= n)
			{
				break;
			}

			//只有end2越界，修改end2的下标，使其指向数据的最后一个元素的下标
			if (end2 >= n)
			{
				end2 = n - 1;
			}


			int j = begin1;
			while (begin1 <= end1 && begin2 <= end2)
			{
				if (a[begin1] < a[begin2])
				{
					tmp[j++] = a[begin1++];
				}
				else
				{
					tmp[j++] = a[begin2++];
				}
			}
			//一个区间结束放完了，还有一个区间没完
			while (begin1 <= end1)
			{
				tmp[j++] = a[begin1++];
			}
			while (begin2 <= end2)
			{
				tmp[j++] = a[begin2++];
			}

			//将数据复制到原数组中
			memcpy(a + i, tmp + i, sizeof(int) * (end2 - i + 1));
		}
		gap *= 2;
	}

	free(tmp);
	tmp = NULL;
}

//计数排序
void CountSort(int* a, int n)
{
	//找出最小值和最大值，求出数据的范围
	int i = 0;
	int min = a[0];
	int max = a[0];
	for (i = 0; i < n; i++)
	{
		if (min > a[i])
		{
			min = a[i];
		}
		if (max < a[i])
		{
			max = a[i];
		}
	}
	int range = max - min + 1;

	//创建count数组
	int* count = (int*)calloc(range, sizeof(int));
	if (count == NULL)
	{
		perror("CountSort()::calloc()");
		return;
	}

	//统计
	for (i = 0; i < n; i++)
	{
		count[a[i] - min]++;
	}

	//将数据拷贝回原数组
	i = 0;
	int j = 0;
	for (j=0; j<range; j++)
	{
		
		while (count[j]--)
		{
			a[i++] = j + min;
		}
	}

}